var currentEditTask;

$(document).ready(function () {
    $("body").append('<div class="modal-task" id="taskPanel" style="overflow: inherit"></div>');
    $(".task-stage").sortable({
//            cancel: ".sort-disabled",
        items: ".sort-able",
//            containment: "parent",
        handle: ".icon-move",
//            connectWith: ".task-stage",
        tolerance: 'pointer',
        forcePlaceholderSize: true,
        opacity: 0.8,
        placeholder: 'ui-state-highlight',
        stop:taskStageSort
    }).disableSelection();
    init();
});

function taskStageSort(){
    var ids = '';
    $.each($("#task-stage").find("li"),function(i,u){
        if($(u).find("a").eq(0).data("id")>0){
            ids += $(u).find("a").eq(0).data("id") + ",";
        }
    });
    $.ajax({
        url:web_path + 'project/task/stage_sort.htm',
        type:'post',
        dataType:'json',
        data:{ids:ids,projectId:projectId},
        cache:false,
        success:function(data) {
        },
        error:function(msg) {
            alert(msg);
        }
    });
}

function init(){
    $('[data-toggle="Task_onc"]').click(function (e) {
        $(this).information('Task',{
            onFa:false,
            eles:'add_text',
            List:'add_con_list',
            elem:'Task-t'
        });
        e.stopPropagation();
    });

    $(".Screening_Singular").find("li").on({
        click:function(e){
            $(this).information('Task_list',{
                onFa:true,
                intext:'types',
                elem:'choose_class'

            });
            e.stopPropagation();
        }
    });

    $(document).on('click',function(e){
        var ofs=$(e.target).parents().hasClass("fudong_add_td");
        var ofg=$(e.target).hasClass("fudong_add_td");
        var osg=$(e.target).hasClass("Template_name");
        if(!osg) {
            if(ofs || ofg){
            }
            else{
                $(".fudong_add_td").hide();
            }
        }else{
            $(".fudong_add_td").hide();
            $(e.target).parent().find(".fudong_add_td").show();
        }
    });

    $(document).on("click",".task-input",function(){
        $(this).information('zhunag',{
            elem:'task-input-ul',
            eles:'task-input-ms'
        });
    });

    $(document).on('click',function(e){
        var ofs=$(e.target).parents().hasClass("bb_con");
        var ofg=$(e.target).hasClass("bb_con");
        var osg=$(e.target).hasClass("shows-pak");
        if(!osg) {
            if(ofs || ofg){

            }
            else{
                $(".bb_con").hide();
            }
        }
    });
}

/**
 * 绑定click事件
 */
$(document).on("click",'[data-toggle="drop-select"]',function (e) {
    $(this).information('dropSelect');
    e.stopPropagation();
});

$(document).on("mouseover", "#task_list tr,.task-member-list li", function () {
    $(this).find(".icon-remove2").toggleClass("hide");
    $(this).css("background-color","#e4e4e4");
});

$(document).on("mouseout", "#task_list tr,.task-member-list li", function () {
    $(this).find(".icon-remove2").toggleClass("hide");
    $(this).css("background-color","#ffffff");
});

/**
 * 绑定click事件
 */
$(document).on("click",".drop-select-menu li",function (e) {
    $(this).information('dropOption');
    e.stopPropagation();
});

$(document).on("mouseleave",".drop-div",function(){
    $(this).hide();
});
$(document).on("click",".task-doc .drop-div li a",function(){
    $(this).parents(".drop-div").hide();
});

/* 删除任务 */
$(document).on("click", "#task_list .icon-remove2", function () {
    removeTask($(this).parents("tr"));
});

/**
 * 切换阶段
 */
$(document).on("click", "#task-stage a", function () {
    selectTaskStage(this);
    stages = $(this).data("id");
    $("#periodId").val(stages);
    var txt = $(this).text();
    $("#periodId").next().children().first().text(txt);
    $("#periodId").next().next().find("li").each(function(){
        if($(this).attr("data")==stages){
            $(this).find("a").eq(0).attr("class","selected");
        }
    });
});

$(document).on("click", "#task-stage li", function () {
    var $this = $(this);
    var that = $this.find("a").eq(0);
    selectTaskStage(that);
    stages = that.data("id");
    $("#periodId").val(stages);
    var txt = that.text();
    $("#periodId").next().children().first().text(txt);
    $("#periodId").next().next().find("li").each(function(){
        if(that.attr("data")==stages){
            that.find("a").eq(0).attr("class","selected");
        }
    });
});


/* 里程碑 */
$(document).on("click", ".task-input-img", function () {
    var flag = $(this).attr("src");
    if (flag.indexOf("on")>0){
        flag = flag.replace("on","off");
        $(this).attr("src",flag);
        $(this).data("state","off");
        $(this).attr("title","设置里程碑");
        $(this).next().val(0);
    }else{
        flag = flag.replace("off","on");
        $(this).attr("src",flag);
        $(this).data("state","on");
        $(this).attr("title","取消里程碑");
        $(this).next().val(1);
    }
});

/** 添加子任务按钮*/
$(document).on('click', '.add-sub-task', function (e) {
    var e = e || event, keycode = e.which || e.keyCode;
    if($("#task_list [edit_flag=1]").length ==0){
        addTemplateSubTask($(this).parent());
    }else{
        alert("有未提交的任务，请将未提交的任务提交完成后才能创建新任务！！！");
        $("#task_list [edit_flag=1]").find("input:first")[0].focus();
    }
});

$("#createTaskForm").keyup(function(e){
    var e=e || event,
        keycode=e.which || e.keyCode;
    if(keycode==13){
        $('.createTask').trigger('click');
    }
});

$(".createTask").click(function(){
    var $this = $(this);
    var title = $.trim($this.prev().find("td").eq(0).find("input").last().val());
    if(title == ''){
        alert("任务名称必填");
    }else{
        $.ajax({
            type : "POST",
            url: web_path+"project/task/saveOrUpdate.htm",
            data:$('#createTaskForm').serialize(),
            async: false,
            dataType : "json",
            cache:false,
            success : function(data) {
                if(data.flag==1){
                    var periodId = $("#periodId").val();
                    var majorId = $("#majorId").val();
                    var subId = $("#subId").val();
                    var periodName = $("#periodId").next().find("span").eq(0).text();
                    if(periodId == -11){
                        periodId = -1;
                    }
                    if(periodName == '全部' || periodName == '阶段'){
                        periodName = '默认';
                    }
                    var majorName= $("#majorId").next().find("span").eq(0).text();
                    var subName = $("#subId").next().find("span").eq(0).text();
                    var taskChargeName = $("#taskChargeName").val();
                    var expectFinishDate = $("#expectFinishDate").val();
                    var html = '<tr class="parent-task" data-stage="'+periodId+'" data-id="'+data.id+'" data-pid="0" data-major="'+majorId+'" data-sub="'+subId+'">';
                    html +='<td><div class="task-input-ms f_l mt5"><div type="text" class="task-input shows-pak"><img src="'+pic+'ta2.png" class="shows-pak" data-ondis="undefined"/></div>';
                    html +='<ul class="task-input-ul bb_con" style="display: none;">';
                    html +='<li data="'+pic+'ta2.png" onclick="changeStatus(2,'+data.id+','+majorId+')"><img src="'+pic+'ta2.png"/>待做</li>';
                    html +='<li data="'+pic+'ta1.png" onclick="changeStatus(1,'+data.id+','+majorId+')"><img src="'+pic+'ta1.png"/>进行中</li>';
                    html +='<li data="'+pic+'ta3.png" onclick="changeStatus(3,'+data.id+','+majorId+')"><img src="'+pic+'ta3.png"/>暂停</li>';
                    html +='<li data="'+pic+'ta4.png" onclick="changeStatus(4,'+data.id+','+majorId+')"><img src="'+pic+'ta4.png"/>已完成</li></ul></div>';
                    html +='<div class="ml40 content-2line" style="display:block;margin-top: 5px;" title="'+data.no +' '+title+'"><a data-toggle="modal-task" data-target="#taskPanel" data-url="'+web_path+'project/task/detail/'+orgId+'/'+projectId+'/'+data.id+'">'+data.no+' '+title+'</a></div></td>';
                    html +='<td class="add-sub-task">+子任务</td><td><div class="content-2line" style="display: block" title="'+taskChargeName+'">'+taskChargeName+'</div></td>';
                    html +='<td><span class="task-tag periodName">'+periodName+'</span>';
                    if(subName != '' && subName !='子项' && subName != '全部'){
                        html += '<span class="task-tag subName">'+subName+'</span>';
                    }else{
                        html += '<span class="subName"></span>';
                    }
                    if(majorName != ''&& majorName !='专业' && majorName != '全部'){
                        html += '<span class="task-tag majorName">'+majorName+'</span>';
                    }else{
                        html += '<span class="majorName"></span>';
                    }
                    html +='</td><td>'+expectFinishDate+'</td>';
                    html +='<td onclick="setpriority(1,'+data.id+','+majorId+')"><img data-State="yes" class="task-input-img" src="'+pic+'qi_off.png" title="设置里程碑"/></td>';
                    html +='<td><p class="icon-remove2 hide"></p></td></tr>';
                    if($("#task_list").find("tr").length == 0){
                        $("#task_list").html(html);
                    }else{
                        $("#task_list").find("tr").eq(0).before(html);
                    }
                    $this.prev().find("td").eq(0).find("input").last().val('');
                    //$("#periodId").val(-1);
                    $("#majorId").val(-1);
                    $("#subId").val(-1);
                    //$("#periodId").next().find("span").eq(0).text('阶段');
                    $("#majorId").next().find("span").eq(0).text('专业');
                    $("#subId").next().find("span").eq(0).text('子项');
                    $("#expectFinishDate").val('');
                    $("#expectFinishDate").next().text("截止日期");
                    var tmpTaskId = data.id;
                    if(taskIds == ''){
                        taskIds = tmpTaskId.toString();
                    }else{
                        taskIds = tmpTaskId + "," + taskIds;
                    }
                    taskIdArr = taskIds.split(",");
                }else{
                    alert("创建失败")
                }
            }
        });
    }
});

$(document).on("keyup",".createSubTask",function(e){

    var e=e || event,keycode=e.which || e.keyCode;
    if(keycode==13){
        createSubTask();
    }
});

function createSubTask() {
    var title = $.trim($("#task_list [edit_flag=1]").find("input").eq(0).val());
    if (title == '') {
        alert("子任务名称必填");
    } else {
        $.ajax({
            type: "POST",
            url: web_path + "project/task/saveSubTask.htm",
            data: $('#createSubTaskForm').serialize(),
            async: false,
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data.flag == 1) {
                    var periodId = $("#periodId_0").val();
                    var majorId = $("#majorId_0").val();
                    var taskChargeName = $("#taskChargeName_0").val();
                    var expectFinishDate = $("#expectFinishDate_0").val();
                    var html = '<tr data-parent="1" class="sub-task" data-stage="' + periodId + '" data-id="' + data.id + '" data-pid="' + data.pid + '">';
                    html += '<td colspan="2"><div class="task-input-ms f_l mt5"><div type="text" class="task-input shows-pak"><img src="' + pic + 'ta2.png" class="shows-pak"/></div>';
                    html += '<ul class="task-input-ul bb_con" style="display: none;">';
                    html += '<li data="' + pic + 'ta2.png" onclick="changeStatus(2,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta2.png"/>待做</li>';
                    html += '<li data="' + pic + 'ta1.png" onclick="changeStatus(1,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta1.png"/>进行中</li>';
                    html += '<li data="' + pic + 'ta3.png" onclick="changeStatus(3,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta3.png"/>暂停</li>';
                    html += '<li data="' + pic + 'ta4.png" onclick="changeStatus(4,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta4.png"/>已完成</li></ul></div>';
                    html += '<div class="ml40 content-2line" style="display: block;margin-top: 5px;" title="' + data.no + ' / ' + title + '"><a data-toggle="modal-task" data-target="#taskPanel" data-url="' + web_path + 'project/task/detail/' + orgId + '/' + projectId + '/' + data.pid + '">' + data.no + '</a>&nbsp;/&nbsp';
                    html += '<a data-toggle="modal-task" data-target="#taskPanel" data-url="' + web_path + 'project/task/detail/' + orgId + '/' + projectId + '/' + data.id + '">' + title + '</a></div></td>';
                    html += '<td><div class="content-2line" style="display: block" title="' + taskChargeName + '">' + taskChargeName + '</div></td><td></td>';
                    html += '<td>' + expectFinishDate + '</td>';
                    html += '<td onclick="setpriority(1,' + data.id + ',' + majorId + ')"><img data-State="yes" class="task-input-img" src="' + pic + 'qi_off.png" title="设置里程碑"/></td>';
                    html += '<td><p class="icon-remove2 hide"></p></td></tr>';
                    $("#task_list [edit_flag=1]").before(html);
                   /* $("#task_list [edit_flag=1]").focus();*/
                    $("#task_list [edit_flag=1]").remove();
                    //获取父任务在数组中的位置
                    var len = $("#task_list [data-pid="+data.pid+"]").length;
                    var index = $.inArray(data.pid.toString(),taskIdArr);
                    var tmpSubTaskId = data.id;
                    //将新增的子任务id添加到数组中父任务对应位置的后面
                    taskIdArr.splice(index+len,0,tmpSubTaskId);
                    $(window).focus();
                } else {
                    alert("创建失败")
                }
            }
        });
    }
}
function selectExpectFinishDate(){
    $dp.$('expectFinishDate').value=$dp.cal.getDateStr('yyyy-MM-dd');
}

function subSelectExpectFinishDate(){
    var index = $(this).prev().attr("id").split("_")[1];
    $dp.$('expectFinishDate_'+index).value=$dp.cal.getDateStr('yyyy-MM-dd');
}

/**
 * 添加子任务
 * @param ptask
 */
function addTemplateSubTask(obj) {
    var html = $("#tmpSubTask").find("tr").eq(0).html();
    var ptask = $(obj);
    var pid = ptask.data("id");
    var pmajor = ptask.data("major");
    var psub = ptask.data("sub");
    var pstage = ptask.data("stage");
    html = '<tr class="sub-task" edit_flag="1" data-id="0" data-pid="'+pid+'" data-stage="'+stages+'">' + html + '</tr>';
    var subTasks = $("#task_list [data-pid=" + pid + "]");
    var insertObj;
    if (subTasks.length > 0) {
        insertObj = $("#task_list [data-pid=" + pid + "]:last");
        insertObj.after(html);
    } else {
        insertObj = $("#task_list [data-id=" + pid + "]");
        insertObj.after(html);
    }
    $("#pid_0").val(pid);
    $("#periodId_0").val(pstage);
    $("#subId_0").val(psub);
    $("#majorId_0").val(pmajor);
    $("#task_list [edit_flag=1]").find("input:first")[0].focus();
    //var html = "<tr class='sub-task'><td colspan='2'>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>";
    //$("#task_list [data-pid="+pid+"]:last").after(html);
}

/**
 * 保存模板任务
 */
function saveTemplateTask() {
    $("#task_list [edit_flag=1]").each(function(i){
        $(this).find("input[type='text']").each(function(j){
            var val = $(this).val();
            var placeholder = $(this).attr("placeholder");
            if (val==""){
                val = placeholder;
            }
            if (placeholder.indexOf("%")>0 && val.indexOf("%")<0){
                $(this).parent().find("span").html(val+"%");
            }else {
                $(this).parent().find("span").html(val);
            }
        })
        $(this).find("span").show();
        $(this).find("input").hide();
        $(this).attr("edit_flag", "0");
    });
}

/**
 * 添加任务负责人
 * @param obj
 */
function taskfzr(obj){
    var picurl = $(obj).attr("picurl");
    $(obj).parent().parent().prev().attr("style","background:url('"+picurl+"') no-repeat scroll 0 2px;padding-left: 15px;background-size:12px,15px;");
    var id = $(obj).attr("data");
    var name = $(obj).find("a").eq(0).text();
    $("#taskChargeId").val(id);
    $("#taskChargeName").val(name);
}

/**
 * 添加子任务负责人
 * @param obj
 * @param userId
 * @param taskCharge
 */
function addSubTaskCharge(obj,userId,taskCharge){
    var index = $(obj).parent().prev().attr("id").split("_")[1];
    var picurl = $(obj).attr("picurl");
    $("#taskChargeName_"+index).val(taskCharge);
    $("#userId_"+index).val(userId);
    $(obj).parent().parent().prev().attr("style","background:url('"+picurl+"') no-repeat scroll 0 12px;padding-left: 15px;background-size:12px,15px; ");
}

//创建任务选择子项、阶段和专业
function choice(obj,category,type){
    var val = $(obj).attr("data");
    if(category == 'projectSub'){
        if(type=='fenTask'){
            $("#projectSubId").val(val);
        }else{
            $("#subId").val(val);
        }
    }else if(category == 'projectStage'){
        if(type=='fenTask'){
            $("#projectPeriodId").val(val);
        }else {
            $("#periodId").val(val);
        }
    }else if(category == 'projectMajor'){
        if(type=='fenTask'){
            var index = $(obj).attr("id");
            $("#majorId_"+index).val(val);
            $("#majorId_name_"+index).val($(obj).attr("ff"));
        }else{
            $("#majorId").val(val);
        }
    }
}

//创建任务选择子项、阶段和专业
function subTaskChoice(obj,category){
    var val = $(obj).attr("data");
    var index = $(obj).parent().prev().attr("id").split("_")[1];
    if(category == 'projectSub'){
        $("#subId_"+index).val(val);
    }else if(category == 'projectStage'){
        $("#periodId_"+index).val(val);
    }else if(category == 'projectMajor'){
        $("#majorId_"+index).val(val);
    }
}

/**
 * 删除任务
 * obj 任务tr对象
 */
function removeTask(obj) {
    var that = $(obj);
    var tid = that.data("id");
    var pid = that.data("pid");
    if(tid == 0){
        that.remove();
    }else{
        var pids = tid;
        if (pid == 0) {
            $("#task_list [data-pid=" + tid + "]").each(function(){
                pids += "," + $(this).data("id");
            });
        }
        $("#pids").val(pids);
        $.ajax({
            url: web_path+'project/task/getAssociateTaskDocumentCount.htm',
            type: 'post',
            dataType: 'json',
            data:{
                'pids':pids
            },
            cache: false,
            success: function (data) {
                if (data.flag == 1) {
                    if (data.count > 0) {
                        $(".remove_taolun").text("该任务下已有输出文档，不可删除！");
                        $("#delButton").hide();
                    } else {
                        $(".remove_taolun").text("该任务下的所有文档和评论将同时删除。如果您完成了这个任务，通常选择将该任务标记为已完成状态，而不是删除。您是否确定删除？");
                    }
                    $("#remove_man").modalopen();
                }
            },
            error: function (msg) {
                alert(msg);
            }
        });
    }
}

function delTask(obj){
    var pids = $(obj).next().next().val();
    $.ajax({
        url:web_path+'project/task/deleteTask.htm',
        type:'post',
        dataType:'json',
        data:{"pids":pids,"type":"projectTask"},
        cache:false,
        success:function(data) {
            if(data.flag == 1){
                //$("#remove_man").modalclose();
                //var arr = pids.split(",");
                //$.each(arr,function(key,val){
                //    $("#task_list [data-id=" + val + "]").remove();
                //});
                window.location.reload();
            }else{
                alert("删除任务失败！！！");
            }
        },
        error:function(msg) {
            alert(msg);
        }
    });
}

/**
 * 添加阶段
 */
function addTemplateTaskStageById(sid, sname) {
    if ($("#task-stage [data-id=" + sid + "]").length == 0) {
        $.ajax({
            url: web_path+'project/task/saveProjectStage.htm',
            type: 'post',
            dataType: 'json',
            data:{
                'major':0,
                'persons':0,
                'stage':sid,
                'projectId':projectId
            },
            cache: false,
            success: function (data) {
                if(data.flag == 1){
                    taskStageSort();
                    var html = '<li onclick="filter(this,\'more\');"><a data-id="' + sid + '" title="'+sname+'" style="padding-top:5px;"><div class="content-2line" style="font-size: 14px;">' + sname + '</div></a><span class="icon-remove2" onclick="deleteTaskStage(this);"></span></li>';
                    $("#task-stage li:last").after(html);
                    selectTemplateTaskStage($("#task-stage a:last"));
                    stages = sid;
                    tmpStages = sid;
                    $("#projectStage").val("");
                    queryData();
                }else{
                    alert("添加失败");
                }
            },
            error: function (msg) {
                alert(msg);
            }
        });
    }
}

/**
 * 选择模板阶段
 */
function selectTemplateTaskStage(stageObj) {
    var sid = $(stageObj).data("id");
    var stxt = $(stageObj).text();
    $("#task-percent").parent().find("label").html(stxt + " 的阶段");
    $(stageObj).parents("ul").children().removeClass("selected");
    var aa = $(stageObj).parents("li");
    $(stageObj).parents("li").addClass("selected");
}

/**
 * 选择阶段
 */
function selectTaskStage(stageObj) {
    var sid = $(stageObj).data("id");
    var stxt = $(stageObj).text();
    $(stageObj).parents("ul").children().removeClass("selected");
    $(stageObj).parents("ul").find(".icon-remove2").hide();
    $(stageObj).parents("li").addClass("selected");
    $(stageObj).parents("li").find(".icon-remove2").show();
}

/**
 * 添加阶段
 */
$(document).on("keypress", "#projectStage", function (e) {
    var e = e || event,
        keycode = e.which || e.keyCode;
    if (keycode == 13) {
        var that = $(this);
        var txt = $.trim(that.val());
        var isRepeat = false;
        if(txt == ''){
            showPrompt("阶段必填");
            isRepeat = true;
        }
        that.next().find("li").each(function () {
            if ($(this).text() == txt) {
                isRepeat = true;
                return;
            }
        });
        if (!isRepeat) {
            $.ajax({
                url: web_path+'project/set/saveProjectStage.htm',
                type: 'post',
                dataType: 'json',
                data:{
                    'name':that.val(),
                    'type':2,
                    'orgId':orgId
                },
                cache: false,
                success: function (data) {
                    if(data.flag == 1){
                        taskStageSort();
                        var id = data.id;
                        stages = id;
                        addTemplateTaskStageById(id,that.val());
                        var html = '<li data-id="'+id+'" onclick="addTemplateTaskStage(this)">'+that.val()+'</li>';
                        that.next().find(".fudong_add_ul").children().last().before(html);
                        that.next().hide();
                    }else{
                        showPrompt("添加失败");
                    }
                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }
    }
});

/**
 * 添加阶段
 */
function addTemplateTaskStage(obj) {
    var sid = $(obj).data("id");
    var sname = $(obj).text();
    addTemplateTaskStageById(sid, sname);
    $(obj).parents(".input_dowm").hide();
}

/**
 * 下拉过滤
 */
function filter(obj,type,val){
    param = '';
    if(type=='more'){
        var isCheck = $(obj).attr("checked");
        if($(obj).parent().attr("id")=='task-stage'){
            if(isCheck!=undefined){
                $(obj).removeAttr("checked");
            }else{
                $(obj).attr("checked","checked");
            }
            $(obj).siblings().each(function () {
                $(this).removeAttr("checked");
            });

            var that = $(obj).find("a").eq(0);
            stages = that.eq(0).data("id");
            if(stages == -11){
                stages = '';
            }

            selectTaskStage(that);
            $("#periodId").val(stages);
            var txt = that.text();
            $("#periodId").next().children().first().text(txt);
            $("#periodId").next().next().find("li").each(function(){
                if(that.attr("data")==stages){
                    that.find("a").eq(0).attr("class","selected");
                }
            });
        }else{
            if(isCheck!=undefined){
                $(obj).removeAttr("checked");
            }else{
                $(obj).attr("checked","checked");
            }
        }
    }else if(type=='member'){
        if($(obj).children().first().hasClass("selected")){
            projectMember = 0;
        }else{
            projectMember = val;
        }

    }else if(type=='projectStatus'){
        var flag = false;//任务筛选是否选中 默认为未选中
        if(val != 6 && val != 7){
            $("#taskStatus").css("visibility","hidden");
        }else{
            $("#taskStatus").removeAttr("style");
        }
        var isCheck = $(obj).attr("checked");

        if(isCheck!=undefined){
            $(obj).removeAttr("checked");
        }else{
            $(obj).attr("checked","checked");
        }

        $(obj).siblings().each(function () {
            $(this).removeAttr("checked");
        });
        $("#prostatus li").each(function () {
            if ($(this).attr("checked") == "checked") {
                flag = true;
                projectStatus = $(this).attr("data");
            }
        });
        if($("input[type='checkbox']").is(':checked') && flag){
            projectStatus = 8;
        }else if($("input[type='checkbox']").is(':checked') && !flag){
            projectStatus = 7;
        }else if(!$("input[type='checkbox']").is(':checked') && !flag){
            projectStatus = 0;
        }else if(!$("input[type='checkbox']").is(':checked') && flag){
            projectStatus = projectStatus;
        }
    }else if(type=='sort'){
        sortColumn=val;
        sortMethod=$(obj).attr("data-sortmethod");
    }

    keyword=$("#keyword").val();
    $("#start").val(0);
    var  pass_ul_flag = false;
    var  pass_ul2_flag = false;
    $.each($("#pass_ul li"),function(i,u){
        if($(u).attr("checked")=="checked"){
            pass_ul_flag = true;
            subIds += $(u).attr("data")+",";
        }
    });
    $.each($("#pass_ul2 li"),function(i,u){
        if($(u).attr("checked")=="checked"){
            pass_ul2_flag = true;
            majors += $(u).attr("data")+",";
        }
    });

    if(!pass_ul_flag){
        $("#projectSub").text('').text('全部');
    }
    if(!pass_ul2_flag){
        $("#major").text('').text('全部');
    }

    queryData();
    tmpSubIds = subIds;
    tmpStages = stages;
    tmpMajors = majors;

    subIds = '';
    stages = '';
    majors = '';
}

/**
 * 异步查询列表
 */
function queryData(){
    $.ajax({
        url:web_path+'project/task/list.htm',
        type:'post',
        dataType:'text',
        data:{"orgId":orgId,"id":projectId,"start":$("#start").val(),"subIds":subIds,"customerId":projectMember,"status":projectStatus,"stages":stages,"majors":majors},
        cache:false,
        success:function(data) {
            $(".task-list").empty();
            $(".task-list").html(data);
            init();
        },
        error:function(msg) {
            alert(msg);
        }
    });
}

//更新任务状态
function changeStatus(status,id,majorId){
    $.ajax({
        url:web_path+'project/task/saveOrUpdate.htm',
        type:'post',
        dataType:'json',
        data:{"status":status,"id":id,"fkId":projectId,"majorId":majorId},
        cache:false,
        success:function(data) {
            if(data.flag==1){
                var par = $("#task_list [data-id="+id+"]").data("parent");
                if(status == 4){
                    $("#task_list [data-id="+id+"]").find("td:not('.add-sub-task')").each(function(i){
                        if(i==0 && par == 0){
                            $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);padding-left:0;");
                            $(this).find("a").attr("style","color: rgb(204,204,204);");
                        }else if(i == 0 && par == 1){
                            $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                            $(this).find("a").attr("style","color: rgb(204,204,204);");
                        }else{
                            $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                            $(this).find("input").eq(0).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                        }
                    });
                    var mydate = getNowFormatDate();
                    $("#task_list [data-id="+id+"]").find("td").last().prev().prev().text(mydate);
                }else{
                    $("#task_list [data-id="+id+"]").find("td").each(function(i){
                        if(i == 0 && par == 0){
                            $(this).attr("style","padding-left:0;");
                            $(this).find("a").attr("style","");
                        }else if(i == 0 && par == 1){
                            $(this).attr("style","");
                            $(this).find("a").attr("style","");
                        }else{
                            $(this).attr("style","");
                            $(this).find("input").eq(0).attr("style","");
                        }

                    });
                    $("#task_list [data-id="+id+"]").find("td:last").prev().prev().text('');
                }

                init();
            }else{
                alert("更新状态失败！！！")
            }
        },
        error:function(msg) {
            alert(msg);
        }
    });

}

//设置或取消里程碑
function setpriority(priority,id,majorId){
    $.ajax({
        url:web_path+'project/task/saveOrUpdate.htm?'+Math.random(),
        type:'post',
        dataType:'json',
        data:{"priority":priority,"id":id,"fkId":projectId,"majorId":majorId},
        cache:false,
        success:function(data) {
            if(data.flag==1){
                if(priority == 0){
                    $("#task_list [data-id="+id+"]").find("td").last().prev().attr("onclick","setpriority(1,"+id+","+majorId+")");
                }else{
                    $("#task_list [data-id="+id+"]").find("td").last().prev().attr("onclick","setpriority(0,"+id+","+majorId+")");
                }
                init();
            }else{
                alert("更新状态失败！！！")
            }
        },
        error:function(msg) {
            alert(msg);
        }
    });
}

/**
 * 分页查询
 * @param page
 */
function gotoPage(page){
    var limit = $("#limit").val();
    var start = (page-1)*limit;
    $("#start").val(start);
    subIds = tmpSubIds;
    majors = tmpMajors;
    stages = tmpStages
    queryData();
}

/**
 * 更新文档标签
 */
function updateDoc(fkType, fkId, id){
	$.ajax({
        url:web_path+'doc/updateDoc.htm',
        type:'post',
        dataType:'json',
        data:{"projectId":projectId,'taskId':id,'fkType':fkType,'fkId':fkId},
        cache:false,
        success:function(data) {
            if(data.flag==1){
                
            }else{
                alert("更新失败！！！")
            }
        },
        error:function(msg) {
            alert(msg);
        }
    });
}

/** 任务详情页 js start */
/**
 * 更新任务状态
 * @param cat
 * @param obj
 * @param id
 * @param planName
 */
function updateTask(cat,obj,id,planName){
    var status = '';
    var majorId = '';
    var periodId = '';
    var subId = '';
    var expectStartDate = '';
    var expectFinishDate = '';
    var startTime = '';
    var completeTime = '';
    var majorName = '';
    var periodName = '';
    var subName = '';
    if(cat == 'status'){
        status = obj;
        majorId = $("#taskMajorId").val();
        periodId = $("#taskPeriodId").val();
        subId = $("#taskSubId").val();
    }else if(cat == 'majorId'){
        majorId = obj;
        $("#taskMajorId").val(majorId);
        $(".subTaskMajor").val(majorId);
        periodId = $("#taskPeriodId").val();
        subId = $("#taskSubId").val();
        majorName = planName;
        if($("#task_list [data-id="+id+"]").hasClass("parent-task")){
            if(planName != '默认'){
                $("#task_list [data-id="+id+"]").find("td").eq(3).find(".majorName").addClass("task-tag").text(planName);
            }else{
                $("#task_list [data-id="+id+"]").find("td").eq(3).find(".majorName").removeClass("task-tag").text('');
            }
        }
    }else if(cat == 'periodId'){
        periodId = obj;
        $("#taskPeriodId").val(periodId);
        $(".subTaskPeriod").val(periodId)
        majorId = $("#taskMajorId").val();
        subId = $("#taskSubId").val();
        periodName = planName;
        if($("#task_list [data-id="+id+"]").hasClass("parent-task")){
            $("#task_list [data-id="+id+"]").find("td").eq(3).find(".periodName").text(planName);
        }
    }else if(cat == 'subId'){
        subId = obj;
        $("#taskSubId").val(subId);
        $(".subTaskSub").val(subId);
        majorId = $("#taskMajorId").val();
        periodId = $("#taskPeriodId").val();
        subName = planName;
        if($("#task_list [data-id="+id+"]").hasClass("parent-task")){
            if(planName != '默认'){
                $("#task_list [data-id="+id+"]").find("td").eq(3).find(".subName").addClass("task-tag").text(planName);
            }else{
                $("#task_list [data-id="+id+"]").find("td").eq(3).find(".subName").removeClass("task-tag").text('');
            }
        }
    }else if(cat == 'expectStartDate'){
        expectStartDate = $(obj).text();
        majorId = $("#taskMajorId").val();
        periodId = $("#taskPeriodId").val();
        subId = $("#taskSubId").val();
        var txt = $(obj).next().text();
        if(txt == '无'){
            $(obj).next().text(expectStartDate);
        }
    }else if( cat == 'expectFinishDate'){
        expectFinishDate = $(obj).text();
        majorId = $("#taskMajorId").val();
        periodId = $("#taskPeriodId").val();
        subId = $("#taskSubId").val();
        var txt = $(obj).prev().text();
        if(txt == '无'){
            $(obj).prev().text(expectFinishDate);
        }
        if($("#task_list [data-id="+id+"]").hasClass("parent-task")){
            if($("#task_list [data-id="+id+"]").find("td").eq(4).attr("style")=='' || $("#task_list [data-id="+id+"]").find("td").eq(4).attr("style") == undefined){
                $("#task_list [data-id="+id+"]").find("td").eq(4).text(expectFinishDate);
            }
        }else{
            if($("#task_list [data-id="+id+"]").find("td").eq(3).attr("style")=='' || $("#task_list [data-id="+id+"]").find("td").eq(3).attr("style") == undefined){
                $("#task_list [data-id="+id+"]").find("td").eq(3).text(expectFinishDate);
            }
        }
    }else if(cat == 'startTime' ){
        startTime = $(obj).text();
        majorId = $("#taskMajorId").val();
        periodId = $("#taskPeriodId").val();
        subId = $("#taskSubId").val();
    }else if(cat == 'completeTime'){
        completeTime = $(obj).text();
        majorId = $("#taskMajorId").val();
        periodId = $("#taskPeriodId").val();
        subId = $("#taskSubId").val();
        $("#taskTitle").attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
        $("#currentAssociateId").next().find("img").eq(0).attr("src",pic+"ta4.png");
        if($("#task_list [data-id="+id+"]").hasClass("parent-task")){
            $("#task_list [data-id="+id+"]").find("td").eq(4).text(completeTime);
        }else{
            $("#task_list [data-id="+id+"]").find("td").eq(3).text(completeTime);
        }
        $("#task_list [data-id="+id+"]").find("td").eq(0).find("img").eq(0).attr("src",pic+"ta4.png");
        var par = $("#task_list [data-id="+id+"]").data("parent");
        $("#task_list [data-id="+id+"]").find("td:not('.add-sub-task')").each(function(i){
            if(i==0 && par == 0){
                $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);padding-left:0;");
                $(this).find("a").attr("style","color: rgb(204,204,204);");
            }else if(i == 0 && par == 1){
                $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                $(this).find("a").attr("style","color: rgb(204,204,204);");
            }else{
                $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                $(this).find("input").eq(0).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
            }
        });
    }
    if(id != null && id != 0){
        $.ajax({
            url:web_path+'project/task/saveOrUpdate.htm',
            type:'post',
            dataType:'json',
            data:{'status':status,'completeTime':completeTime,
                'startTime':startTime,'expectFinishDate':expectFinishDate,'expectStartDate':expectStartDate,
                'subId':subId,'id':id,'periodId':periodId,'majorId':majorId,"fkId":projectId,"majorName":majorName,"subName":subName,"periodName":periodName},
            cache:false,
            success:function(data) {
                if(data.flag==1){
                    var mydate = getNowFormatDate();
                    if(status == 4){
                        if(planName!=undefined && planName != ''){
                            $("#"+planName).text(mydate);
                            $("#taskTitle").attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                        }else{
                            $("#sub_task_list [data-id="+id+"]").text(mydate);
                        }
                        if($("#task_list [data-id="+id+"]").hasClass("parent-task")){
                            $("#task_list [data-id="+id+"]").find("td").eq(4).text(mydate);
                        }else{
                            $("#task_list [data-id="+id+"]").find("td").eq(3).text(mydate);
                        }
                        $("#task_list [data-id="+id+"]").find("td").eq(0).find("img").eq(0).attr("src",pic+"ta4.png");
                        var par = $("#task_list [data-id="+id+"]").data("parent");
                        $("#task_list [data-id="+id+"]").find("td:not('.add-sub-task')").each(function(i){
                            if(i==0 && par == 0){
                                $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);padding-left:0;");
                                $(this).find("a").attr("style","color: rgb(204,204,204);");
                            }else if(i == 0 && par == 1){
                                $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                                $(this).find("a").attr("style","color: rgb(204,204,204);");
                            }else{
                                $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                                $(this).find("input").eq(0).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                            }
                        });
                    }else if(status != '' && status !=4){
                        $("#taskTitle").attr("style","");
                        $("#task_list [data-id="+id+"]").find("td").eq(0).find("img").eq(0).attr("src",pic+"ta"+status+".png");
                        var par = $("#task_list [data-id="+id+"]").data("parent");
                        $("#task_list [data-id="+id+"]").find("td:not('.add-sub-task')").each(function(i){
                            $(this).attr("style","");
                            $(this).find("a").attr("style","");
                            $(this).find("input").eq(0).attr("style","");
                        });
                    }
                    init();
                }else{
                    alert("更新失败！！！")
                }
            },
            error:function(msg) {
                alert(msg);
            }
        });
    }
}

function updateSubTaskExpectStartDate(){
    updateSubTask(this);
}

function updateSubTask(obj){
    var expectFinishDate = $(obj).text();
    var id = $(obj).data("id");
    if(id != null && id != 0){
        $.ajax({
            url:web_path+'project/task/saveOrUpdate.htm',
            type:'post',
            dataType:'json',
            data:{'id':id,"fkId":projectId,'expectFinishDate':expectFinishDate},
            cache:false,
            success:function(data) {
                if(data.flag==1){
                    $("#task_list [data-id="+id+"]").find("td").eq(3).text(expectFinishDate);
                }else{
                    alert("更新失败！！！")
                }
            },
            error:function(msg) {
                alert(msg);
            }
        });
    }
}

function setPriority(id,majorId){
    if(priority == 0){
        priority = 1;
    }else{
        priority = 0;
    }
    $.ajax({
        url:web_path+'project/task/saveOrUpdate.htm?'+Math.random(),
        type:'post',
        dataType:'json',
        data:{"priority":priority,"id":id,"fkId":projectId,"majorId":majorId},
        cache:false,
        success:function(data) {
            if(data.flag==1){
                if(priority == 0){
                    $("#task_list [data-id="+id+"]").find("td").last().prev().attr("onclick","setpriority(1,"+id+","+majorId+")");
                }else{
                    $("#task_list [data-id="+id+"]").find("td").last().prev().attr("onclick","setpriority(0,"+id+","+majorId+")");
                }

                var $this = $("#task_list [data-id="+id+"]").find("td").last().prev().find("img").eq(0);
                if($this.length > 0){
                    var flag = $this.attr("src");
                    if (flag.indexOf("on")>0){
                        flag = flag.replace("on","off");
                        $this.attr("src",flag);
                        $this.data("state","off");
                        $this.attr("title","设置里程碑");
                    }else{
                        flag = flag.replace("off","on");
                        $this.attr("src",flag);
                        $this.data("state","on");
                        $this.attr("title","取消里程碑");
                    }
                }

                $("#activity_con").activityLoad({"orgId":orgId,"type":type,"id":id,"site":web_path,"nodeId":""});
            }else{
                alert("更新状态失败！！！")
            }
        },
        error:function(msg) {
            alert(msg);
        }
    });
    init();
}

/**
 * 任务详情页 更新子任务负责人
 * @param obj
 * @param id
 * @param name
 * @param taskId
 * @param expectFinishDate
 */
function updateSubTaskCharge(obj,id,name,taskId,expectFinishDate){
    if(taskId != null && taskId != 0){
        $.ajax({
            url: web_path+'project/task/saveOrUpdate.htm',
            type: 'post',
            dataType: 'json',
            data: {'id': taskId,"userId": id,"taskChargeName":name,"operator":"updateTaskCharger","expectFinishDate":expectFinishDate,"fkId":projectId},
            cache: false,
            success: function (data) {
                if (data.flag ==0) {
                    showPrompt("更新失败！！！")
                }else{
                    $("#task_list [data-id="+taskId+"]").find("td").eq(1).find("div").eq(0).text(name);
                }
            },
            error: function (msg) {
                alert(msg);
            }
        });
    }
}

/* 任务负责人 */
function updateTaskfzr(id,name,taskId,obj){
    var taskChargeTd = $("#chargeId").val();
    var canyurens = $("#canyurens").val();
    taskId = $("#taskId").val();
    var that = $(obj);
    var picurl = that.attr("picurl");
    var majorId = $("#taskMajorId").val();
    var periodId = $("#taskPeriodId").val();
    var subId = $("#taskSubId").val();
    $.ajax({
        url: web_path+'project/task/saveOrUpdate.htm',
        type: 'post',
        dataType: 'json',
        data: {'subId': subId, 'id': taskId, 'periodId': periodId, 'majorId': majorId, "fkId":projectId, "userId": id,"taskChargeName":name,"operator":"updateTaskCharger"},
        cache: false,
        success: function (data) {
            if (data.flag ==0) {
                alert("更新失败！！！")
            }else if (data.flag==1){
                $("#chargeId").val(id);
                that.parent().parent().prev().find("img").eq(0).attr("src",picurl);
                if($("#task_list [data-id="+taskId+"]").hasClass("parent-task")){
                    $("#task_list [data-id="+taskId+"]").find("td").eq(2).find("div").eq(0).text(name);
                }else{
                    $("#task_list [data-id="+taskId+"]").find("td").eq(1).find("div").eq(0).text(name);
                }
                init();
            }
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

/**
 * 删除任务参与人
 * @param userId
 */
function removecyr(userId,taskId,obj){
    var canyurens = $("#canyurens").val();
    if(isContain(userId,canyurens)){
        $("#canyurens").val(strDel(userId,canyurens));
    }
    var majorId = $("#taskMajorId").val();
    var periodId = $("#taskPeriodId").val();
    var subId = $("#taskSubId").val();
    var that = $(obj);
    $.ajax({
        url:web_path+'project/task/saveOrUpdate.htm',
        type:'post',
        dataType:'json',
        data:{'subId':subId,'id':taskId,'periodId':periodId,'majorId':majorId,"fkId":projectId,"userId":userId},
        cache:false,
        success:function(data) {
            if(data.flag==1){
                that.parent().parent().remove();
                init();
            }else{
                alert("更新失败！！！")
            }
        },
        error:function(msg) {
            alert(msg);
        }
    });
}

/**
 * 添加参与人
 * @param obj
 * @param userId
 * @param userName
 * @returns {boolean}
 */
function anfuzhi(obj,userId,userName){
    var that = $(obj);
    var taskId = $("#taskId").val();
    //var name0 = $("#chargeId").val();     //负责人名称
    //if (name0 == userId) {
    //    alert("此人已是负责人");
    //    return false;
    //}
    var canyurens = $("#canyurens").val();
    if (isContain(userId, canyurens)) {
        alert("此人已是参与人");
    } else {
        var majorId = $("#taskMajorId").val();
        var periodId = $("#taskPeriodId").val();
        var subId = $("#taskSubId").val();
        $.ajax({
            url: web_path+'project/task/saveOrUpdate.htm',
            type: 'post',
            dataType: 'json',
            data: {'subId': subId, 'id': taskId, 'periodId': periodId, 'majorId': majorId, "fkId":projectId, "userId": userId,"taskChargeName":userName,"operator":"addParticipater"},
            cache: false,
            success: function (data) {
                if (data.flag == 0) {
                    alert("更新失败！！！")
                }else if(data.flag ==1){
                    var data = that.attr("data");
                    var picurl = that.attr("picurl");
                    var str = "";
                    str += '<li class="clearfix"><div class="avatar"><img src="'+picurl+'" width="30px" height="30px" title="'+userName+'"></div>';
                    str += '<div class="name">'+userName+'<span class="icon-remove2 f_r mt10 mr10 hide" onclick="removecyr('+userId+','+taskId+',this);"></span></div></li> ';
                    $(".line-height35").children().last().prev().children().last().after(str);
                    canyurens += "," + userId ;
                    $("#canyurens").val(canyurens);
                    init();
                }
            },
            error: function (msg) {
                alert(msg);
            }
        });

    }
}

$(document).on("click",".border-gray",function(){
    var that = $(this);
    var process = 0;
    if(that.find("input").length>0){
        process = that.find("input").eq(0).val();
    }else{
        process = $(this).text().replace("%","");
    }
    var html = '<input class="w45 process" type="text" value="'+process+'" placeholder="'+process+'%" style="display: inline;">';
    $(this).html(html);
    $(this).find("input").focus();
});

$(document).on("blur",".process",function(){
    var that = $(this);
    var process = that.val();
    var taskId = $("#taskId").val();
    $.ajax({
        url:web_path+"project/task/updateTaskProcess.htm",
        type: 'post',
        dataType: 'json',
        data: {'id': taskId, 'taskStep':process},
        cache: false,
            success: function (data) {
            if (data.flag == 0) {
                alert("更新失败！！！")
            }else if(data.flag ==1){
                $(".border-gray").html(process + "%");
                init();
            }
        },
        error: function (msg) {
            alert(msg);
        }
    });

});
/**
 * 设置进度
 */
$(document).on('keyup', '.process', function () {
    isNumOrMoney(this, 1);
});

function updateTaskExpectFinishDate(){
    $dp.$('expectFinishDate').value=$dp.cal.getDateStr('yyyy-MM-dd');
    updateTask('expectFinishDate',this,$("#taskId").val());
}

function updateTaskCompleteTime(){
    updateTask('completeTime',this,$("#taskId").val());
}

function updateTaskExpectStartDate(){
    $dp.$('expectStartDate').value=$dp.cal.getDateStr('yyyy-MM-dd');
    updateTask('expectStartDate',this,$("#taskId").val());
}

function addSubTask(){
    var str = $("#createSubTask").find("tr").eq(0).html();
    str="<tr>"+str+"</tr>";
    $("#sub_task_list").children().last().after(str);
    if($("#sub_task_list").children().length == 0){
        $("#sub_task_list").html(str);
    }else{
        $("#sub_task_list").children().last().find("td").eq(1).find("input")[0].focus();
    }
}

$(document).on("blur",".taskInput",function(){
    var $this = $(this);
    var title = $.trim($this.val());
    if(title != ''){
        saveSubTask(this);
    }
});

function saveSubTask(obj){
    var $this = $(obj);
    var src = $("#sub_task_list").children().last().find("td").eq(0).find("img").eq(0).attr("src");
    var status = src.substring(src.length-5,src.length-4);
    var that = $("#sub_task_list").children().last().find("td").eq(2).find(".selected");
    var userId = that.parent().attr("data");
    var taskChargeName = that.text();
    var title = $(obj).val();
    $.ajax({
        url: web_path+"project/task/insertSubTask.htm",
        type: 'post',
        data:{
            'title':title,
            'id':$("#taskId").val(),
            'status':status,
            'userId':userId,
            'taskChargeName':taskChargeName
        },
        dataType: 'json',
        cache: false,
        success: function (data) {
            if(data.flag == 1){
                var id = data.id ;
                $("#sub_task_list").children().last().find("td").eq(0).find("li").each(function(i){
                    if(i==0){
                        $(this).attr("onclick","updateTask('status',2,"+id+")");
                    }else if(i==1){
                        $(this).attr("onclick","updateTask('status',1,"+id+")");
                    }else{
                        $(this).attr("onclick","updateTask('status',"+(i+1)+","+id+")");
                    }
                });
                $("#sub_task_list").children().last().find("td").eq(2).find("li").each(function(){
                    var param = $(this).attr("onclick");
                    $(this).attr("onclick",param.substring(0,param.length-8)+id + ",null);")
                });
                $("#sub_task_list").children().last().find("td").last().prev().find("span").attr("data-id",id);
                $this.prev().text(title);
                $this.prev().show();
                $this.remove();

                //将新建的子任务添加到列表中 start

                var periodId = $("#taskPeriodId").val();
                var majorId = $("#taskMajorId").val();
                var expectFinishDate = $.trim($("#sub_task_list").children().last().find("td").last().prev().find("span").text());
                if(expectFinishDate == '计划完成时间'){
                    expectFinishDate = '';
                }
                var html = '<tr data-parent="1" class="sub-task" data-stage="' + periodId + '" data-id="' + data.id + '" data-pid="' + data.pid + '">';
                html += '<td colspan="2"><div class="task-input-ms f_l mt5"><div type="text" class="task-input shows-pak"><img src="' + pic + 'ta'+status+'.png" class="shows-pak" data-ondis="undefined"/></div>';
                html += '<ul class="task-input-ul bb_con" style="display: none;">';
                html += '<li data="' + pic + 'ta2.png" onclick="changeStatus(2,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta2.png"/>待做</li>';
                html += '<li data="' + pic + 'ta1.png" onclick="changeStatus(1,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta1.png"/>进行中</li>';
                html += '<li data="' + pic + 'ta3.png" onclick="changeStatus(3,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta3.png"/>暂停</li>';
                html += '<li data="' + pic + 'ta4.png" onclick="changeStatus(4,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta4.png"/>已完成</li></ul></div>';
                html += '<div class="ml40 content-2line" style="display: block;margin-top: 5px;" title="' + data.no + ' / ' + title + '"><a data-toggle="modal-task" data-target="#taskPanel" data-url="' + web_path + 'project/task/detail/' + orgId + '/' + projectId + '/' + data.pid + '">' + data.no + '</a>&nbsp;/&nbsp';
                html += '<a data-toggle="modal-task" data-target="#taskPanel" data-url="' + web_path + 'project/task/detail/' + orgId + '/' + projectId + '/' + data.id + '">' + title + '</a></div></td>';
                html += '<td><div class="content-2line" style="display: block" title="' + taskChargeName + '">' + taskChargeName + '</div></td><td></td>';
                html += '<td>' + expectFinishDate + '</td>';
                html += '<td onclick="setpriority(1,' + data.id + ',' + majorId + ')"><img data-State="yes" class="task-input-img" src="' + pic + 'qi_off.png" title="设置里程碑"/></td>';
                html += '<td><p class="icon-remove2 hide"></p></td></tr>';
                if($("#task_list [data-pid="+data.pid+"]").length == 0){
                    $("#task_list [data-id="+data.pid+"]").after(html);
                }else{
                    $("#task_list [data-pid="+data.pid+"]:last").after(html);
                }
                //将新建的子任务添加到列表中 end


                $("#sub_task_list").children().last().find("td").last().html('');
                var len = $("#task_list [data-pid="+data.pid+"]").length;
                var index = $.inArray(data.pid.toString(),taskIdArr);
                //将新增的子任务id添加到数组中父任务对应位置的后面
                taskIdArr.splice(index+len,0,id);
                //console.log(taskIdArr);

            }
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

/**
 * 详情页 添加子任务负责人
 * @param obj
 * @param userId
 * @param taskCharge
 */
function addSubTaskCharger(obj,userId,taskCharge){
    var index = $(obj).parent().prev().attr("id").split("_")[1];
    var picurl = $(obj).attr("picurl");
    $("#chargeName_"+index).val(taskCharge);
    $("#taskUserId_"+index).val(userId);
    $(obj).parent().parent().prev().attr("style","background:url('"+picurl+"') no-repeat scroll 0 12px;padding-left: 15px;background-size:12px,15px; ");
    $(obj).parent().parent().find("div").eq(0).find("input").eq(0).val('');
    $(obj).parent().find("li").show();
}

function subTaskSelectExpectFinishDate(){
    var index = $(this).prev().attr("id").split("_")[1];
    $dp.$('taskExpectFinishDate_'+index).value=$dp.cal.getDateStr('yyyy-MM-dd');
}

function delSubTask(obj){
    $(obj).parent().parent().remove();
}

$(document).on("click",".icon-previous",function(){
    var taskId = $(this).data("id");
    var nextTaskId = taskIdArr[0];
    var index = 0;
    for(var i = 0 ; i< taskIdArr.length; i++){
        if(taskIdArr[i] == taskId){
            index = i;
        }
    }
    if(index == taskIdArr.length - 1){
        nextTaskId = taskIdArr[index];
    }else{
        nextTaskId = taskIdArr[index+1];
    }
    $.ajax({
        url: web_path+"project/task/detail/"+orgId+"/"+projectId+"/"+nextTaskId,
        type: 'post',
        dataType: 'text',
        cache: false,
        success: function (data) {
            $("#taskPanel").empty();
            $("#taskPanel").html(data);
            $("#taskPanel").opentask();
            init();
        },
        error: function (msg) {
            alert(msg);
        }
    });
    //$(this).attr("href",web_path+"project/task/detail/"+orgId+"/"+projectId+"/"+nextTaskId);
});

$(document).on("click",".icon-next",function(){
    var taskId = $(this).data("id");
    var previousTaskId = taskIdArr[0];
    var index = 0;
    for(var i = 0 ; i< taskIdArr.length; i++){
        if(taskIdArr[i] == taskId){
            index = i;
        }
    }
    if(index == 0){
        previousTaskId = taskIdArr[index];
    }else{
        previousTaskId = taskIdArr[index-1];
    }
    $.ajax({
        url: web_path+"project/task/detail/"+orgId+"/"+projectId+"/"+previousTaskId,
        type: 'post',
        dataType: 'text',
        cache: false,
        success: function (data) {
            $("#taskPanel").empty();
            $("#taskPanel").html(data);
            $("#taskPanel").opentask();
            init();
        },
        error: function (msg) {
            alert(msg);
        }
    });
    //$(this).attr("href",web_path+"project/task/detail/"+orgId+"/"+projectId+"/"+previousTaskId);
});

$(document).on("click",".subTask",function(){
    $.ajax({
        type : "POST",
        url: web_path+"project/task/saveSubTask.htm",
        data:$('#subTaskForm').serialize(),
        async: false,
        dataType : "json",
        cache:false,
        success : function(data) {
            if(data.flag==1){
            }else{
                alert("创建失败")
            }
        }
    });
});

function editRequireRemarkOrContent(obj,type) {
    var content = $(obj).text();
    $(obj).hide();
    var html = '';
    if(content == '点击此处添加交付要求' || content == '点击此处添加描述'){
        html = '<textarea placeholder="' + content + '" style="width:85%;vertical-align: text-top;" id="'+type+'" onblur="updateRequireRemarkOrContent(this,\''+type+'\')"></textarea>';
    }else{
        html = '<textarea placeholder="' + content + '" style="width:85%;vertical-align: text-top;" id="'+type+'" onblur="updateRequireRemarkOrContent(this,\''+type+'\')">'+content+'</textarea>';
    }
    var h = $(obj).parent().html();
    $(obj).parent().html(h + html);
    $("#"+type).focus();
}

function updateRequireRemarkOrContent(obj,type){
    var that = $(obj);
    var content = $.trim(that.val());

    var param = '';
    if(type == 'taskRequireRemark'){
        param = {'requireRemark':content,'id':$("#taskId").val()};
    }else{
        param = {'content':content,'id':$("#taskId").val()};
    }
    $.ajax({
        url: web_path+'project/task/updateTaskProcess.htm',
        type: 'post',
        dataType: 'json',
        data:param,
        cache: false,
        success: function (data) {
            if (data.flag == 1) {
                if(content == ''){
                    if(type == 'taskRequireRemark'){
                        content = '点击此处添加交付要求';
                    }else{
                        content = '点击此处添加描述';
                    }
                }
                that.prev().html(content);
                that.prev().show();
                that.remove();
            }
        },
        error: function (msg) {
            alert(msg);
        }
    });

}

function detailDelTask(str){
    $("#pids").val(str);
    $.ajax({
        url: web_path+'project/task/getAssociateTaskDocumentCount.htm',
        type: 'post',
        dataType: 'json',
        data:{
            'pids':str
        },
        cache: false,
        success: function (data) {
            if (data.flag == 1) {
                if (data.count > 0) {
                    $(".remove_taolun").text("该任务下已有输出文档，不可删除！");
                    $("#delButton").hide();
                } else {
                    $(".remove_taolun").text("该任务下的所有文档和评论将同时删除。如果您完成了这个任务，通常选择将该任务标记为已完成状态，而不是删除。您是否确定删除？");
                }
            }
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

$(document).click(function(e){
    $dp.hide();
});

function findTaskChargeOrParticipates(obj){
    var key = $.trim($(obj).val());
    $(obj).parent().parent().find("a").each(function(){
        var txt = $(this).text();
        if(!contains(txt,key,true)){
            $(this).parent().hide();
        }
    });
    if(key == ''){
        $(obj).parent().parent().find("li").show();
    }
}

/**
 * 对当前时间进行格式化
 * @returns {string}
 */
function getNowFormatDate() {
    var day = new Date();
    var Year = 0;
    var Month = 0;
    var Day = 0;
    var CurrentDate = "";
    //初始化时间
    //Year= day.getYear();//有火狐下2008年显示108的bug
    Year= day.getFullYear();//ie火狐下都可以
    Month= day.getMonth()+1;
    Day = day.getDate();
    //Hour = day.getHours();
    // Minute = day.getMinutes();
    // Second = day.getSeconds();
    CurrentDate += Year + "-";
    if (Month >= 10 ){
        CurrentDate += Month + "-";
    }else{
        CurrentDate += "0" + Month + "-";
    }
    if (Day >= 10 ){
        CurrentDate += Day ;
    }else{
        CurrentDate += "0" + Day ;
    }
    return CurrentDate;
}

function isNumOrMoney(s, type) {
    //清除“数字”和“.”以外的字符
    if (type == 2) {
        s.value = s.value.replace(/[^\d]/g, "");
    } else if (type == 1) {
        s.value = s.value.replace(/[^\d.]/g, "");
        s.value = s.value.replace(/^\./g, "");  //验证第一个字符是数字而不是.
        s.value = s.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
        s.value = s.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    }
}